package cn.acewill.yxd.yunxundian.ui.schedule;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.adapter.ListitemAdapter;
import cn.acewill.yxd.yunxundian.adapter.selectMailAdapter;
import cn.acewill.yxd.yunxundian.pubClass.SimpleSpinnerOption;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;

/**
 * Created by xxx on 2018/1/5.
 */

public class MultMailActivity extends BaseActivity implements TextWatcher{

    private  selectMailAdapter mailAdapter;
    private EditText searchFor;
    private Handler handler=new Handler();
    private List<SimpleSpinnerOption> searchList;
    private ListView myList;
    //数据源
    private List<SimpleSpinnerOption> mailList = new ArrayList<>();
    //记录上个页面的选择情况
    private List<SimpleSpinnerOption> selList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mult_mail_view);
        initView();
    }
    private void initView(){
        int whatReceiver = getIntent().getIntExtra("target", 1);
        String str = getIntent().getStringExtra("str");
        String select = getIntent().getStringExtra("selects");
        Gson gson=new Gson();
        Type typeList = new TypeToken<ArrayList<SimpleSpinnerOption>>() {
        }.getType();
        mailList = gson.fromJson(pubClass.convertToArrayJsonNormal(str),typeList);
        if (!select.equals("null") && !select.equals("[]")){
            selList = gson.fromJson(pubClass.convertToArrayJsonNormal(select),typeList);
        }else{
            selList = mailList;
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.myToolBar);
        toolbar.setTitle("");
        toolbar.setNavigationIcon(R.mipmap.back);
        setSupportActionBar(toolbar);
        TextView title = (TextView) findViewById(R.id.title_bar);
        searchFor = (EditText) findViewById(R.id.searchFor);
        searchFor.addTextChangedListener(this);
        if (whatReceiver == 1){
            title.setText("选择收件人");
        }else{
            title.setText("选择抄送人");
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
         myList = (ListView) findViewById(R.id.myList);
         mailAdapter = new selectMailAdapter(this,selList);
        myList.setAdapter(mailAdapter);
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SimpleSpinnerOption option = (SimpleSpinnerOption) parent.getItemAtPosition(position);
                if (option.isChecked()){
                    option.setChecked(false);
                }else{
                    option.setChecked(true);
                }
                mailAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.muti_select_ok,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_ok:
                if (mailAdapter!=null){
//                    List<SimpleSpinnerOption> selects = mailAdapter.getSelectList();
//                    if (selects!=null&&selects.size()>0){
//                        for (SimpleSpinnerOption option: selects) {
//                            Log.e("simple",option.getName()+","+option.getValue());
//                            Intent in = new Intent();
//                            in.putExtra("selectStr", JSON.toJSONString(selects));
//                            setResult(102,in);
//                        }
//                    }
                    List<SimpleSpinnerOption> selects = mailAdapter.getAllList();
                    if (selects!=null&&selects.size()>0){
                        Intent in = new Intent();
                        in.putExtra("selectStr",JSON.toJSONString(selList));
                        setResult(102,in);
                    }
                    finish();
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        handler.post(textSearch);
    }
    Runnable textSearch = new Runnable() {
        @Override
        public void run() {
            String text = searchFor.getText().toString().trim();
            getSearchData(text);
            mailAdapter = new selectMailAdapter(MultMailActivity.this,searchList);
            myList.setAdapter(mailAdapter);
        }
    };
    public void getSearchData(String text){
        if (selList!=null&&selList.size()>0){
            searchList = new ArrayList<>();
            for (SimpleSpinnerOption option:selList) {
                if (option.getName().contains(text)){
                    searchList.add(option);
                }
            }
        }
    }
}
